/**
 *  * Copyright (c) 2025. joleye.com all rights reserved..
 * 工具栏操作控件，异步编辑 0.1.2
 * 异步编辑工具
 * @anther joleye
 * https://github.com/joleye/toolbar
 */
define(['jquery'], function ($) {
    $.fn.batchEdit = function (option) {

        this.each(function () {
            $(this).attr('title', '可编辑');
            $(this).click(function () {
                bindClick.apply(this);
            });
        });

        function bindClick() {
            $(this).addClass('s-edit');
            var type = $(this).data("type");
            var dom;
            var event = null;
            if (type === "bool") {
                dom = document.createElement("input");
                $(dom).attr("type", "checkbox");
                if ($(this).html() === "是") {
                    $(dom).val("1");
                } else {
                    $(dom).val("0");
                    $(dom).attr("checked", "");
                }
                dom.focus();
                $(dom).on('click', function () {
                    if (this.checked) {
                        this.value = "1";
                    } else {
                        this.value = "0";
                    }
                });
                event = 'blur';
            } else if (type === 'date') {
                dom = document.createElement("input");
                $(dom).addClass("cxcalendar-input");
                var dateWidth = $(this).css('width');
                dateWidth && $(dom).css('width', dateWidth);
                $(dom).attr('readonly', true);
                $(dom).val($(this).text().trim());
                if ($.fn.cxCalendar) {
                    $(dom).cxCalendar({
                        type: 'date',
                        format: 'YYYY-MM-DD',
                    }, function () {
                        $('.cxcalendar_lock').click(function () {
                            blurFunc.call(dom, that);
                        });
                    });
                    event = 'change';
                } else {
                    console.warn('not init cxCalendar');
                }
            } else if (type === 'select') {
                var ref = $(this).data('ref');
                dom = $($('#' + ref).html()).appendTo($(this));
                var selectWidth = $(this).css('width');
                selectWidth && $(dom).css('width', selectWidth);
                $(dom).val($(this).data('value'));
                event = 'blur';
            } else {
                var width = $(this).css('width');
                var height = parseInt($(this).css('height') || 25);
                var html = $(this).html();
                dom = document.createElement("textarea");
                $(dom).addClass("i-edit-textarea");
                width && $(dom).css('width', width);
                var line = html.split('\n');
                $(dom).css({'height': (height * line.length) + 'px', 'line-height': height + 'px'});
                $(dom).val(html.trim());
                $(dom).on('input', function () {
                    var value = $(this).val();
                    if (value.split('\n').length > line.length) {
                        line = value.split('\n');
                        $(dom).css({'height': (height * line.length) + 'px'});
                    }
                });
                event = 'blur';
            }
            $(this).html("");
            $(dom).appendTo($(this));

            if (type === 'bool') {
                dom.checked = $(dom).val() === "1";
            }

            if (event) {
                var that = this;
                $(dom).on(event, function () {
                    if (type === 'bool') {
                        setTimeout(function (_this, o) {
                            blurFunc.call(_this, o);
                        }, 200, this, that);
                    } else {
                        blurFunc.call(this, that);
                    }
                });
            }

            $(this).unbind('click');
            $(dom).focus();
        }

        function blurFunc(ori) {
            var valid = $(ori).data('valid');
            var field = $(ori).data('field');
            var type = $(ori).data('type');
            var val;
            var text;

            if (type === "int") {
                text = $(this).val();
                val = $(this).val();
                if (!/^\d+$/.test(val)) {
                    alert("数据类型错误~~~");
                    return;
                }
            } else if (type === "bool") {
                text = $(this).val();
                val = $(this).val();
                if (!/^0|1$/.test(val)) {
                    alert("数据类型错误~~~");
                    return;
                }
            } else if (type === 'select') {
                text = $(this).find('option:selected').text();
                val = $(this).val();
            } else {
                text = $(this).val();
                val = $(this).val();
            }

            var data = {field: field, id: valid, val: val};
            if (option.params) {
                $.extend(data, option.params.call(ori, this, val));
            }
            $(ori).removeClass('s-edit');
            $(ori).addClass('s-loading');
            $.post(option.url, data, function (res) {
                if (!res.status) {
                    alert(res.msg);
                }
                $(ori).removeClass('s-loading');
            });

            $(ori).data('value', val);
            if (type === 'bool') {
                if (val === "1") {
                    ori.innerHTML = "是";
                } else {
                    ori.innerHTML = "否";
                }
            } else {
                ori.innerHTML = text;
            }

            $(ori).click(function () {
                bindClick.apply(this);
            });
        }
    };
});
